package com.ejuornal.action;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.ejuornal.bean.EditorCommit;
import com.ejuornal.bean.Periodical;
import com.ejuornal.bean.User;
import com.ejuornal.bean.vo.ECUserPeriodical;
import com.ejuornal.service.EditorCommitService;
import com.ejuornal.service.EditorCommitVoService;
import com.ejuornal.service.MessageService;
import com.ejuornal.service.PeriodicalService;
import com.ejuornal.service.UserService;
import com.ejuornal.springutil.SpringUtil;
import com.google.gson.Gson;

public class EditorCommitAction extends BaseAction<EditorCommit> {
	private static final long serialVersionUID = 1L;
	private EditorCommit editorCommit;
	private ECUserPeriodical editorCommitVO;
	private MessageService messageService;
	private EditorCommitService editorCommitService;
	private EditorCommitVoService editorCommitVoService;
	private PeriodicalService periodicalService;
	private List<ECUserPeriodical> editorCommitList;
	private UserService userService;
	private String periodicalId;//期刊id
	private String state;//编委状态
	private String value;//接收值
	private String periodicalStr;//在jsp页中获取期刊下拉列表
	
	/**
	 * 申请编委的jsp页面
	 * @return
	 */
	public String addCommitCheckInfo(){
		return SUCCESS;
	}
	/**
	 * Description 审核时读取编委信息到审核页面
	 * @author lwf
	 * @return 
	 */
	public String readEditorInfoToPage(){
		int editorCommitId = Integer.parseInt(value);
		editorCommitVoService = (EditorCommitVoService) SpringUtil.getBean("editorCommitVoService");
		editorCommitVO = editorCommitVoService.getEditorCommitInfoById(editorCommitId);
		return "success";
	}
	/**
	 * 会员申请编委
	 * @return
	 */
	public String userApplayEditorCommit(){
		editorCommitService = (EditorCommitService) SpringUtil.getBean("editorCommitService");
		editorCommit.setApplyDate(new Date());
		editorCommitService.add(editorCommit);
		if("2".equals(value)) {
			return "space";
		}
		
		return "success";
	}
	
	/**
	 * Decription 添加编委
	 * @author lwf
	 * @return
	 */
	public String addEditorCommit(){
		editorCommitService = (EditorCommitService) SpringUtil.getBean("editorCommitService");
		editorCommit.setApplyDate(new Date());
		editorCommitService.add(editorCommit);
		userService = (UserService) SpringUtil.getBean("userService");
		User user = new User();
		user.setUserId(editorCommit.getUserId());
		user.setRoleId(6);
		userService.updateRoleIdOnly(user);
		if("2".equals(value)) {
			return "space";
		}
		
		return "success";
	}
	
	/**
	 * Description 审核编委
	 * @author lwf
	 * @return
	 */
	public String auditEditorCommit(){
		editorCommitService = (EditorCommitService) SpringUtil.getBean("editorCommitService");
		userService = (UserService) SpringUtil.getBean("userService");	
		//1   通过     -1  不通过
		if(editorCommit.getResult().equals("1")){
			messageService.sendMessage(periodicalService, messageService, userService, editorCommit, "1");
		}else {  //-1
			messageService.sendMessage(periodicalService, messageService, userService, editorCommit, "2");
		}
		
		editorCommitService.update(editorCommit);
		User user = userService.findById(editorCommit.getUserId());
		user.setRoleId(6);
		userService.updateRoleIdOnly(user);
		return SUCCESS;
	}
	
	/**
	 * Description 根据主编id获取所有编委信息 
	 * @author lwf
	 * @return  List<ECUserPeriodical>
	 */
	public String getAllEditorCommit(){
		int editorChiefId = super.getUserIdBySession();//由session获得主编id
		editorCommitVoService = (EditorCommitVoService) SpringUtil.getBean("editorCommitVoService");
		editorCommitList = editorCommitVoService.findAllByEditorChiefId(editorChiefId);
		return "success";
	}
	
	/**
	 * Description 根据主编id获取所管理的期刊编委-状态为未审核的 
	 * @author lwf
	 * @return List<ECUserPeriodical>
	 */
	public String getAllEditorCommitByState(){
		int editorChiefId = super.getUserIdBySession();//由session获得主编id
		int stateInt = Integer.parseInt(state);
		editorCommitVoService = (EditorCommitVoService) SpringUtil.getBean("editorCommitVoService");
		editorCommitList = editorCommitVoService.findAllByECIdState(editorChiefId, stateInt);
		return "success";
	}
	
	/**
	 * Description 根据主编id获取所有管理的期刊信息--下拉框
	 * @author lwf
	 */
	public void getAllPeriodicalByChiefId(){
		int editorChiefId = super.getUserIdBySession();//由session获得主编id
		periodicalService = (PeriodicalService) SpringUtil.getBean("periodicalService");
		List<Periodical> list = periodicalService.findByEditorChiefId(editorChiefId);
		
		Gson gson = new Gson();
		periodicalStr = gson.toJson(list);
		HttpServletResponse res = ServletActionContext.getResponse();   
		res.reset();   
		res.setContentType("text/html;charset=utf-8");   
		PrintWriter pw;
		try {
			pw = res.getWriter();
			pw.print(periodicalStr);
			pw.flush();   
			pw.close();  
		} catch (IOException e) {
			e.printStackTrace();
		}   
	}
	
	
	/**
	 * Description 根据期刊id获取所某一期的编委信息
	 * @author lwf
	 * @return
	 */
	public String getEditorCommitByPeriodicalId(){
		int periodicalIdInt = editorCommitVO.getPeriodicalId();
		editorCommitVoService = (EditorCommitVoService) SpringUtil.getBean("editorCommitVoService");
		editorCommitList =  editorCommitVoService.findAllEditorCommitBiPerId(periodicalIdInt);
		return "geteditorCommitInfoByPerId";
	}
	
	public String isAppliedCommit() {
		int userId = super.getUserIdBySession();
		
		boolean result = editorCommitService.isAppliedCommit(userId);
		
		dataMap.clear();
		dataMap.put("result", result);
		
		return "success";
	}
	
	public EditorCommit getEditorCommit() {
		return editorCommit;
	}
	public void setEditorCommit(EditorCommit editorCommit) {
		this.editorCommit = editorCommit;
	}
	public EditorCommitService getEditorCommitService() {
		return editorCommitService;
	}

	public void setEditorCommitService(EditorCommitService editorCommitService) {
		this.editorCommitService = editorCommitService;
	}

	public PeriodicalService getPeriodicalService() {
		return periodicalService;
	}

	public void setPeriodicalService(PeriodicalService periodicalService) {
		this.periodicalService = periodicalService;
	}

	public String getPeriodicalId() {
		return periodicalId;
	}

	public void setPeriodicalId(String periodicalId) {
		this.periodicalId = periodicalId;
	}

	public EditorCommitVoService getEditorCommitVoService() {
		return editorCommitVoService;
	}

	public void setEditorCommitVoService(EditorCommitVoService editorCommitVoService) {
		this.editorCommitVoService = editorCommitVoService;
	}

	public List<ECUserPeriodical> getEditorCommitList() {
		return editorCommitList;
	}

	public void setEditorCommitList(List<ECUserPeriodical> editorCommitList) {
		this.editorCommitList = editorCommitList;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public ECUserPeriodical getEditorCommitVO() {
		return editorCommitVO;
	}

	public void setEditorCommitVO(ECUserPeriodical editorCommitVO) {
		this.editorCommitVO = editorCommitVO;
	}

	public String getPeriodicalStr() {
		return periodicalStr;
	}

	public void setPeriodicalStr(String periodicalStr) {
		this.periodicalStr = periodicalStr;
	}

	public MessageService getMessageService() {
		return messageService;
	}
	
	@Resource(name = "messageService")
	public void setMessageService(MessageService messageService) {
		this.messageService = messageService;
	}
}
